package com.zhu.mall.product.service.impl;


import com.zhu.mall.product.service.CategoryBrandRelationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhu.common.utils.PageUtils;
import com.zhu.common.utils.Query;
import com.zhu.mall.product.dao.BrandDao;
import com.zhu.mall.product.entity.BrandEntity;
import com.zhu.mall.product.service.BrandService;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    //分页，并且模糊查询检索
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //获取根据关键字查询的key
        String key = (String) params.get("key");
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            wrapper.eq("brand_id",key).or().like("name",key);
        }

        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                new QueryWrapper<BrandEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void updateDetail(BrandEntity brand) {
        //保证冗余字段一致
        //先把自己品牌表里的字段更新，其次再更新其他关联表中的数据
        this.updateById(brand);
        //判断当前品牌表的品牌名是否为空，不为空则更新其他关联表里的数据（品牌名）
        if(!StringUtils.isEmpty(brand.getName())){
            categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());

            //todo 更新其他关联
        }

    }

}